{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "#load \"..\\Extension\\LoadBaseTool.csx\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "vscode": {
          "languageId": "yaml"
        }
      },
      "source": [
        "Parse 函数抛出异常\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "try\n",
        "{\n",
        "    uint _ret = uint.Parse(\"已\");\n",
        "    _ret.Dump();\n",
        "}\n",
        "catch (Exception ex)\n",
        "{\n",
        "    ex.Message.Dump();\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "try\n",
        "{\n",
        "    DateTime _time = DateTime.Parse(\"202410311015\");\n",
        "    _time.Dump();\n",
        "}\n",
        "catch (Exception ex)\n",
        "{\n",
        "    ex.Message.Dump();\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "class A \n",
        "{\n",
        "    public int _data = 10;\n",
        "}\n",
        "\n",
        "A _a = new A();\n",
        "List<A> _a_list = [ new A()];\n",
        "A _a1 = _a_list[0];"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "Func<uint> _function=null;\n",
        "_function += ()=>{ return 1;};\n",
        "\n",
        "uint? _result = _function?.Invoke();"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "#r \"..\\Extension\\BaseHSMSTool\\BaseHSMSTool.dll\"\n",
        "using BaseHSMSTool;"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[Int32:System.Collections.Generic.List`1+Enumerator[BaseHSMSTool.SECSItem]]\r\n"
          ]
        }
      ],
      "source": [
        "SECSItem _item = new SECSItem(10);\n",
        "Console.WriteLine(_item);"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "1\r\n"
          ]
        }
      ],
      "source": [
        "byte _byte = (byte)1.0f;\n",
        "_byte.Dump();"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "True\r\n"
          ]
        }
      ],
      "source": [
        "byte[] _byte_array = default;\n",
        "(_byte_array is null).Dump();"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0\r\n"
          ]
        }
      ],
      "source": [
        "byte _byte = default;\n",
        "_byte.Dump();"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "-127\r\n"
          ]
        }
      ],
      "source": [
        "unchecked\n",
        "{\n",
        "    // 129的二进制是10000001，转换为sbyte后，最高位是符号位，所以是负数\n",
        "    sbyte _sbyte = (sbyte)129;\n",
        "    _sbyte.Dump();\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Value was either too large or too small for a signed byte.\r\n"
          ]
        }
      ],
      "source": [
        "try\n",
        "{\n",
        "    // 128 超过了sbyte的范围，使用Convert.ToSByte会抛出异常\n",
        "    Convert.ToSByte(\"128\").Dump();\n",
        "}\n",
        "catch (Exception ex)\n",
        "{\n",
        "    ex.Message.Dump();\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "张三,30,周六\n",
            "李四,40,\n",
            "王五,50,\n"
          ]
        }
      ],
      "source": [
        "class Item\n",
        "{\n",
        "    public string Name {get;init;}\n",
        "    public int Age {get; set;}\n",
        "    public string Remark {get;set;}\n",
        "}\n",
        "\n",
        "List<Item> _list = \n",
        "[\n",
        "    new Item() {Name = \"张三\", Age = 30},\n",
        "    new Item() {Name = \"李四\", Age = 40},\n",
        "    new Item() {Name = \"王五\", Age = 50},\n",
        "];\n",
        "\n",
        "// 使用First方法找到第一个Age为30的Item，这里是引用类型，所以修改后_list中的数据也会被修改·\n",
        "Item _item = _list.First(_item => _item.Age == 30);\n",
        "_item.Remark = \"周六\";\n",
        "foreach (var _item in _list)\n",
        "{\n",
        "    $\"{_item.Name},{_item.Age},{_item.Remark}\".Dump();\n",
        "}\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 26,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "1. (1, Hello World)\n",
            "2. \n"
          ]
        }
      ],
      "source": [
        "(uint _id, string _message)? _tuple = (1, \"Hello World\");\n",
        "Console.WriteLine($\"1. {_tuple}\");\n",
        "\n",
        "(uint _id, string _message)? _tuple1 = null;\n",
        "Console.WriteLine($\"2. {_tuple1}\");"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        },
        "polyglot_notebook": {
          "kernelName": "csharp"
        }
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": ".NET (C#)",
      "language": "C#",
      "name": ".net-csharp"
    },
    "language_info": {
      "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
      "kernelInfo": {
        "defaultKernelName": "csharp",
        "items": [
          {
            "aliases": [],
            "languageName": "csharp",
            "name": "csharp"
          }
        ]
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
